const { serverUrl,devUrl } = require('./config.js');

let basicUrl = serverUrl;

const GET = (url,query)=>{
	return new Promise(async (resolve, reject) => {
		try {
			let result = await uni.request({
				url: `${basicUrl}/${url}`,
				method: "GET",
				data: query,
			});
			resolve(result[1].data);
		} catch(err){
			uni.showToast({
				title:"网络连接异常，请检查网络连接",
				duration:3000,
				icon:"none"
			})
			reject('网络连接异常');
		}
	});
}

const POST = (url,data)=>{
	return new Promise(async (resolve, reject) => {
		try {
			let result = await uni.request({
				url: `${basicUrl}/${url}`,
				method: "POST",
				data: data,
			});
			resolve(result[1].data);
		} catch(err){
			console.log(err);
			uni.showToast({
				title:"网络连接异常，请检查网络连接",
				duration:3000,
				icon:"none"
			})
			reject('网络连接异常');
		}
	});
}

const POST_TOKEN = (url,data)=>{
	let token = uni.getStorageSync("token");
	data.token = token;
	return new Promise(async (resolve, reject) => {
		try {
			let result = await uni.request({
				url: `${basicUrl}/${url}`,
				method: "POST",
				data: data,
			});
			resolve(result[1].data);
		} catch(err){
			uni.showToast({
				title:"网络连接异常，请检查网络连接",
				duration:3000,
				icon:"none"
			})
			reject('网络连接异常');  
		}
	});
}

module.exports = {
	GET,
	POST,
	POST_TOKEN
}